package com.hnkywl.sts.action.tjbb;

import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.tjbb.YdcbdbService;
import com.hnkywl.sts.util.POIUtil;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: SDE09
 * Date: 13-10-18
 * Time: 上午11:12
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/tjbb/ydcbdb")
public class YdcbdbAction extends SimpleActionSupport {
    private YdcbdbService ydcbdbService;
    private Integer gsid;
    private Date begindt;
    private Date enddt;
    private Integer jylb;
    private Integer xslx;
    private String custname;
    private Integer startrow;
    private Integer endrow;
    private int page;
    private int rows;
    private GnczService gnczService;
    private List<Gncz> gnczList;
    //private Integer cxfw;
  /*  private Long defaultJg;
    private String defaultJgMc;*/

    /**
     * 移动成本对比表
     */
    @Action("ydcbdbList")
    public void ywsfcList() {
        if (begindt == null) {
            begindt = new Date();
            Calendar calendar = Calendar.getInstance();
            //得到月初
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            begindt = calendar.getTime();
        }
        if (enddt == null) {
            enddt = new Date();
        }
        if (jylb == null) {
            jylb = -1;
        }
        if (xslx == null) {
            xslx = -1;
        }
        if (custname == null || "".equals(custname))
            custname = "-1";
        String json = ydcbdbService.ydcbdbList(gsid, begindt, enddt, jylb, xslx, custname, page, rows);
        renderJson(json);
    }

    @Action("ydcbdb")
    public String ydcbdb() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        Calendar calendar = Calendar.getInstance();
        //得到月初
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        begindt = calendar.getTime();
        enddt = new Date();
      // cxfw = YgService.getCurrentYgCxfw();
        //如果查询范围为全部不带入初始值，如果不是全部只能查询到本公司或本部门
        /*if (cxfw != 0) {
            defaultJg = YgService.getCurrentYgZgGsId() == 1 ?
                    YgService.getCurrentYgGsId() : YgService.getCurrentYgZgGsId();
            defaultJgMc = YgService.getCurrentYgZgGsId() == 1 ?
                    YgService.getCurrentYgGs() : YgService.getCurrentYgZgGs();
        }*/
        return SUCCESS;
    }
    /**
     * 移动成本对比表导出
     */
    @Action("exportydcbdb")
    public String exportydcbdb(){
        if (begindt == null) {
            begindt = new Date();
            Calendar calendar = Calendar.getInstance();
            //得到月初
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            begindt = calendar.getTime();
        }
        if (enddt == null) {
            enddt = new Date();
        }
        if (jylb == null) {
            jylb = -1;
        }
        if (xslx == null) {
            xslx = -1;
        }
        if (custname == null || "".equals(custname))
            custname = "-1";
        try {
            custname = URLDecoder.decode(custname, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Map<String,Map> map = ydcbdbService.ydcbdbListExport(gsid, begindt, enddt, jylb, xslx, custname);
        List<Map> result = (List<Map>) map.get("rows");
        List<Map> footer = (List<Map>) map.get("footer");
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet1");
        POIUtil poiUtil = new POIUtil(wb);
        sheet.setDefaultRowHeightInPoints(21);
        sheet.setDefaultColumnWidth((short) 20);
        HSSFCellStyle headerStyle = poiUtil.createHeader3HSSFCellStyle(wb);
        poiUtil.fillRow(sheet, headerStyle, 0,"销售公司", "销售部门",  "销售单号", "出库日期","客户名称","品名","规格型号",
                "图号 ","单位","产地"," 销售数量","销售价 ","销售额","销售成本"," 销售利润","移动成本 ","移动利润");
        //gs_name	bm_name   xsdh	ckrq	cust_name 	pm	ggxh	th	dw	cd	xssl  xsj  xse  xscb  xslr  ydcb   ydlr
        for (int i = 1; i < result.size() + 1; i++) {
            Map item = result.get(i - 1);
            poiUtil.fillRowWithScale(sheet,null,i,4,item.get("gs_name"),item.get("bm_name"),item.get("xsdh"),item.get("ckrq"),
                    item.get("cust_name"),item.get("pm"),item.get("ggxh"),item.get("th"),item.get("dw"),item.get("cd"),
                    item.get("xssl"),item.get("xsj"),item.get("xse"),item.get("xscb"),item.get("xslr"),item.get("ydcb"),
                    item.get("ydlr"));
        }
        HSSFCellStyle sumStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setColor(HSSFFont.COLOR_RED);
        sumStyle.setFont(font);
        poiUtil.fillRowWithScale(sheet,sumStyle,result.size() + 1,4,"合计：","","","","","","","","","",
                footer.get(0).get("xssl"),     footer.get(0).get("xsj"),      footer.get(0).get("xse"),
                footer.get(0).get("xscb"),   footer.get(0).get("xslr"),   footer.get(0).get("ydcb"),
                footer.get(0).get("ydlr"));
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("移动成本对比表.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }
    //===============getter===setter============================

    public Integer getGsid() {
        return gsid;
    }

    public void setGsid(Integer gsid) {
        this.gsid = gsid;
    }

    public Date getBegindt() {
        return begindt;
    }

    public void setBegindt(Date begindt) {
        this.begindt = begindt;
    }

    public Date getEnddt() {
        return enddt;
    }

    public void setEnddt(Date enddt) {
        this.enddt = enddt;
    }

    public Integer getJylb() {
        return jylb;
    }

    public void setJylb(Integer jylb) {
        this.jylb = jylb;
    }

    public Integer getXslx() {
        return xslx;
    }

    public void setXslx(Integer xslx) {
        this.xslx = xslx;
    }

    public String getCustname() {
        return custname;
    }

    public void setCustname(String custname) {
        this.custname = custname;
    }

    public Integer getStartrow() {
        return startrow;
    }

    public void setStartrow(Integer startrow) {
        this.startrow = startrow;
    }

    public Integer getEndrow() {
        return endrow;
    }

    public void setEndrow(Integer endrow) {
        this.endrow = endrow;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public GnczService getGnczService() {
        return gnczService;
    }

    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }



    @Autowired
    public void setYdcbdbService(YdcbdbService ydcbdbService) {
        this.ydcbdbService = ydcbdbService;
    }
}
